
#-*-coding:utf-8-*-#

import os
import numpy as np
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt

currentPath = os.getcwd()
os.chdir('PATH')

EMM = pd.read_csv('Data_Fig2a_1.csv', index_col=0) # Drought data with negative sentiment and network information
D = nx.read_gexf('Data_Fig2a_2.gexf') # Drought graph data

#From the start of awareness increase
Emp_data = EMM[112:]
Emp_data['Count'] = Emp_data['Count'] - Emp_data['Count'][112]
Emp_data = Emp_data.reset_index(drop=True)

degrees = dict(D.degree())

pos_degrees_vals = list(filter(lambda val: val > 0, degrees.values()))
sum(pos_degrees_vals)/len(pos_degrees_vals)
uq_pos_degrees_vals = sorted(set(pos_degrees_vals))
hist = [pos_degrees_vals.count(x) for x in uq_pos_degrees_vals]

x = np.asarray(uq_pos_degrees_vals, dtype = float)
y = np.asarray(hist, dtype = float)

logx = np.log10(x)
logy = np.log10(y)

logk_interp = np.linspace(np.min(logx),np.max(logx[:50]),1000)
logPk_interp = np.interp(logk_interp, logx[:50], logy[:50])
plt.plot(logk_interp, logPk_interp,".")
plt.show()
m, c = np.polyfit(logk_interp, logPk_interp, 1)

plt.figure(figsize=(5,5),dpi=400)
plt.xlim(min(logx), max(logx))
plt.ylim(-0.2, 4)
plt.xlabel('log10 (Degree)')
plt.ylabel('log10 (Number of nodes)')
scatter_plot = plt.plot(logx, logy, 'o', color='k')
plt.plot(logk_interp, m*logk_interp + c, "-",color='orange')
plt.text(1,2,'logy = -2.4 logx + 4.1',fontsize=14)
plt.axhline(y=0,color='black',linestyle='--')
plt.xlim([0,3.5])
plt.tight_layout()
plt.show()